﻿using DevExpress.Data;
using DevExpress.Utils.Serializing;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Registrator;
using DevExpress.XtraGrid.Views.Base;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using XCI.Helper;
using XCI.Setting;

namespace XCI.Windows
{
    [System.ComponentModel.DesignerCategory("Code")]
    public class BandedGridViewPlus : DevExpress.XtraGrid.Views.BandedGrid.BandedGridView, ISettingSerializer, IGridEmptyDataStyle
    {
        public BandedGridViewPlus()
        {
        }

        public BandedGridViewPlus(GridControl grid) : base(grid)
        {
        }

        protected override string ViewName => "BandedGridViewPlus";

        /// <summary>
        /// 是否允许查询面板查询隐藏列
        /// </summary>
        [Category("XCI"), Description("是否允许查询面板查询隐藏列"), DefaultValue(false)]
        [XtraSerializableProperty]
        public virtual bool AllowFindHiddenColumn { get; set; }

        [Category("XCI"), Description("是否允许在未加载到数据时显示消息"), DefaultValue(true)]
        [XtraSerializableProperty]
        public virtual bool AllowEmptyDataMessage { get; set; } = true;

        [Category("XCI"), Description("空数据时显示消息"), DefaultValue("暂未查到数据")]
        [XtraSerializableProperty]
        public virtual string EmptyDataMessage { get; set; } = "暂未查到数据";

        [Category("XCI"), Description("空数据时显示消息颜色")]
        [XtraSerializableProperty]
        public virtual Color EmptyDataMessageColor { get; set; } = Color.Gray;

        private Size? _emptyDataMessageSize;
        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Size EmptyDataMessageSize
        {
            get
            {
                if (!_emptyDataMessageSize.HasValue && !string.IsNullOrEmpty(EmptyDataMessage))
                {
                    _emptyDataMessageSize = WinFormHelper.CalcTextSize(EmptyDataMessage);
                }
                return _emptyDataMessageSize ?? Size.Empty;
            }
        }

        public event EventHandler<CollectionChangeEventArgs> ColumnSortInfoChanged;
        protected override void OnColumnSortInfoCollectionChanged(CollectionChangeEventArgs e)
        {
            base.OnColumnSortInfoCollectionChanged(e);
            ColumnSortInfoChanged?.Invoke(this, e);
        }

        protected override List<IDataColumnInfo> GetFindToColumnsCollection()
        {
            if (AllowFindHiddenColumn)
            {
                return base.GetFindToColumnsCollection().Concat(Columns.Where(c => !c.Visible).Select(CreateIDataColumnInfoForFilter)).ToList();
            }

            return base.GetFindToColumnsCollection();
        }

        #region Implementation of ISettingSerializer

        /// <summary>
        /// 保存控件配置
        /// </summary>
        /// <param name="xmlFile">xml文件路径</param>
        public void SaveLayout(string xmlFile)
        {
            WinFormHelper.SaveLayout(this, xmlFile);
        }

        /// <summary>
        /// 加载配置到控件
        /// </summary>
        /// <param name="xmlFile">xml文件路径</param>
        public void RestoreLayout(string xmlFile)
        {
            WinFormHelper.RestoreLayout(this, xmlFile);
        }

        /// <summary>
        /// 重置控件配置
        /// </summary>
        public void ReseteLayout()
        {

        }

        #endregion
    }

    public class BandedGridViewPlusInfoRegistrator : BandedGridInfoRegistrator
    {
        public override string ViewName => "BandedGridViewPlus";

        public override BaseView CreateView(GridControl grid)
        {
            return new BandedGridViewPlus(grid);
        }
    }
}
